package cylons;

import java.util.Arrays;

public class Hunter {

	private int[] shipSizes;
	private char[][] myPegBoard;
	public char[][] getMyPegBoard() {
		return myPegBoard;
	}

	public void setMyPegBoard(char[][] myPegBoard) {
		this.myPegBoard = myPegBoard;
	}

	private boolean[] shipsRemaining;

	public Hunter(int[] shipSizes, boolean[] shipsRemaining) {
		this.shipSizes = shipSizes;
		this.shipsRemaining = shipsRemaining;
	}

	public Move getNextMove(char[][] myPegBoard) {
		this.myPegBoard = myPegBoard;
		int maxScore = 0;
		Move maxMove = new Move(0,0,Move.Mode.HUNT);
		int[][] scoreArray = new int[10][10];
		for (int row = 0; row < myPegBoard.length; row++) {
			for (int col = 0; col < myPegBoard[row].length; col++) {
				int nextScore = shipFitScore(row,col);
				scoreArray[row][col] = nextScore;
				//System.out.println(nextScore);
				if (nextScore > maxScore) {
					maxScore = nextScore;
					maxMove = new Move(row, col, Move.Mode.HUNT);
				}
			}
			//System.out.println("next row");
		}
		System.out.println(Arrays.deepToString(scoreArray));
		System.out.println(Arrays.deepToString(myPegBoard));
		System.out.println("Maximal move: row " + maxMove.getRow() + " col " + maxMove.getColumn() + " score" + maxScore);
		return maxMove;
	}

	private int shipFitScore(int row, int col) {
		int spaceScore = 0;
		for (int i = 0; i < shipSizes.length; i++) {
			if (shipsRemaining[i]) {
				int shipLength = shipSizes[i];
				spaceScore += shipFitsInSpace(row, col, shipLength);
			}
		}
		return spaceScore;
	}
	
	private int shipFitsInSpace(int row, int col, int shipLength) {
		int spaceScore = 0;
		for (int i=col-shipLength+1; i <= col; i++) {
			//System.out.println("ship fits horizontal " + row + " " + i);
			if (shipFitsHorizontal(row, i, shipLength)) spaceScore++;
		}
		for (int i=row-shipLength+1; i <= row; i++) {
			//System.out.println("ship fits vertical " + row + " " + i);
			if (shipFitsVertical(i, col, shipLength)) spaceScore++;
		}
		return spaceScore;
	}
	
	private boolean shipFitsHorizontal(int row, int col, int shipLength) {
		for (int i = col; i < shipLength+col; i++) {
			if (!isSpaceEmpty(row, i)) {
				return false;
			}
		}
		return true;
	}
	
	private boolean shipFitsVertical(int row, int col, int shipLength) {
		for (int i = row; i < shipLength+row; i++) {
			if (!isSpaceEmpty(i, col)) {
				return false;
			}
		}
		return true;
	}

	private boolean isSpaceEmpty(int row, int col) {
		if (row >= 0 && row < myPegBoard.length && col >= 0
				&& col < myPegBoard.length && myPegBoard[row][col] == ' ') {
			return true;
		} else {
			return false;
		}
	}
	
//	public static void main(String[] args) {
//		//testing harness
//		boolean[] shipsRemaining = {true, true, true};
//		int[] shipLengths = {4,3,2};
//		char[][] board = 	{{' ',' ',' ',' ',' '}
//							,{' ','M',' ',' ',' '}
//							,{' ',' ',' ',' ',' '}
//							,{' ',' ',' ',' ',' '}
//							,{' ',' ',' ',' ',' '}};
//		Hunter hunter = new Hunter(shipLengths, board, shipsRemaining);
//		
//		System.out.println(hunter.shipFitsHorizontal(0, 1, 3));
//		System.out.println(hunter.shipFitsVertical(0, 1, 3));
//		System.out.println(hunter.shipFitScore(1, 1));
//		System.out.println("ship fits in space " + hunter.shipFitsInSpace(0, 4, 3));
//	}

}
